package com.example.todolist.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.todolist.mapper.TeamFarMapper;
import com.example.todolist.model.TeamFarDO;
import com.example.todolist.model.TeamPerDO;
import com.example.todolist.mapper.TeamPerMapper;
import com.example.todolist.service.TeamPerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cqq
 * @since 2022-03-21
 */
@Service
public class TeamPerServiceImpl extends ServiceImpl<TeamPerMapper, TeamPerDO> implements TeamPerService {

    @Resource
    private TeamFarMapper teamFarMapper;
    @Override
    public int addPerToTeam(TeamFarDO teamFarDO) {
      int i =  this.baseMapper.insert(
                TeamPerDO.builder()
                        .teamId(teamFarDO.getTeamId())
                        .planPerUid(teamFarDO.getUid())
                        .uname(teamFarDO.getUname())
                        .uurl(teamFarDO.getUurl())
                        .build()
        );
        return i;
    }

    @Override
    public List<TeamPerDO> selectAll() {
        return this.baseMapper.selectList(null);
    }

    @Override
    public List<TeamPerDO> selectDetails(String teamID) {
        QueryWrapper<TeamPerDO> teamPerDOQueryWrapper = new QueryWrapper<>();
        teamPerDOQueryWrapper.eq("team_id", teamID);
        return this.baseMapper.selectList(teamPerDOQueryWrapper);
    }

    @Override
    public List<TeamPerDO> selectByOneJoinTeam(String openID) {
        QueryWrapper<TeamPerDO> teamPerDOQueryWrapper = new QueryWrapper<>();
        teamPerDOQueryWrapper.eq("plan_per_uid", openID);
        return this.baseMapper.selectList(teamPerDOQueryWrapper);
    }

    @Override
    public List<TeamPerDO> IsJoinTeam(String teamId, String uid) {
        QueryWrapper<TeamPerDO> teamPerDOQueryWrapper = new QueryWrapper<>();
        teamPerDOQueryWrapper.eq("plan_per_uid", uid);
        teamPerDOQueryWrapper.eq("team_id", teamId);
        return this.baseMapper.selectList(teamPerDOQueryWrapper);
    }

    @Override
    public int returnTeam(String id,String openID) {
        QueryWrapper<TeamFarDO> teamFarDOQueryWrapper = new QueryWrapper<>();
        teamFarDOQueryWrapper.eq("id", id);
        TeamFarDO teamFarDO = teamFarMapper.selectOne(teamFarDOQueryWrapper);
        QueryWrapper<TeamPerDO> teamPerDOQueryWrapper = new QueryWrapper<>();
        teamPerDOQueryWrapper.eq("plan_per_uid", openID);
        teamPerDOQueryWrapper.eq("team_id", teamFarDO.getTeamId());
        return this.baseMapper.delete(teamPerDOQueryWrapper);
    }
}
